﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using DTcms.DBUtility;
using DTcms.Common;
using System.Collections.Generic;

namespace DTcms.DAL
{
    /// <summary>
    /// 数据访问类:ch_hangqing
    /// </summary>
    public partial class hangqing
    {
        public hangqing()
        { }
        #region 基本方法============================
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from ch_hangqing");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)};
            parameters[0].Value = id;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Model.hangqing model)
        {
            using (SqlConnection conn = new SqlConnection(DbHelperSQL.connectionString))
            {
                conn.Open();
                using (SqlTransaction trans = conn.BeginTransaction())
                {
                    try
                    {
                        #region 添加主表数据====================
                        StringBuilder strSql = new StringBuilder();
                        strSql.Append("insert into ch_hangqing(");
                        strSql.Append("title,update_time,status,hits,sort_id,province_id,city_id,district_id,img_url,caizhi_id)");
                        strSql.Append(" values (");
                        strSql.Append("@title,@update_time,@status,@hits,@sort_id,@province_id,@city_id,@district_id,@img_url,@caizhi_id)");
                        strSql.Append(";select @@IDENTITY");
                        SqlParameter[] parameters = {
					        new SqlParameter("@title", SqlDbType.NVarChar,100),
					        new SqlParameter("@update_time", SqlDbType.DateTime),
					        new SqlParameter("@status", SqlDbType.TinyInt,1),
					        new SqlParameter("@hits", SqlDbType.Int,4),
					        new SqlParameter("@sort_id", SqlDbType.Int,4),
                            new SqlParameter("@province_id", SqlDbType.Int,4),
                            new SqlParameter("@city_id", SqlDbType.Int,4),
                            new SqlParameter("@district_id", SqlDbType.Int,4),
                            new SqlParameter("@img_url", SqlDbType.NVarChar,255),
                            new SqlParameter("@caizhi_id", SqlDbType.Int,4)};

                        parameters[0].Value = model.title;
                        parameters[1].Value = model.update_time;
                        parameters[2].Value = model.status;
                        parameters[3].Value = model.hits;
                        parameters[4].Value = model.sort_id;
                        parameters[5].Value = model.province_id;
                        parameters[6].Value = model.city_id;
                        parameters[7].Value = model.district_id;
                        parameters[8].Value = model.img_url;
                        parameters[9].Value = model.caizhi_id;

                        //添加主表数据
                        object obj = DbHelperSQL.GetSingle(conn, trans, strSql.ToString(), parameters); //带事务
                        model.id = Convert.ToInt32(obj);
                        #endregion

                        #region 添加商品规格====================
                        if (model.price != null)
                        {
                            StringBuilder strSql6;
                            foreach (Model.hangqing_price modelt in model.price)
                            {
                                strSql6 = new StringBuilder();
                                strSql6.Append("insert into ch_hangqing_price(");
                                strSql6.Append("article_id,title,price,is_up,sort_id)");
                                strSql6.Append(" values (");
                                strSql6.Append("@article_id,@title,@price,@is_up,@sort_id)");
                                SqlParameter[] parameters6 = {
					                    new SqlParameter("@article_id", SqlDbType.Int,4),
                                        new SqlParameter("@title", SqlDbType.NVarChar,100),
                                        new SqlParameter("@price", SqlDbType.NVarChar,50),
                                        new SqlParameter("@is_up", SqlDbType.Int,4),
					                    new SqlParameter("@sort_id", SqlDbType.Int,4)};
                                parameters6[0].Value = model.id;
                                parameters6[1].Value = modelt.title;
                                parameters6[2].Value = modelt.price;
                                parameters6[3].Value = modelt.is_up;
                                parameters6[4].Value = modelt.sort_id;

                                DbHelperSQL.GetSingle(conn, trans, strSql6.ToString(), parameters6); //带事务
                            }
                        }
                        #endregion

                        trans.Commit();
                    }
                    catch
                    {
                        trans.Rollback();
                        return 0;
                    }
                }
            }
            return model.id;
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Model.hangqing model)
        {
            using (SqlConnection conn = new SqlConnection(DbHelperSQL.connectionString))
            {
                conn.Open();
                using (SqlTransaction trans = conn.BeginTransaction())
                {
                    try
                    {
                        #region 修改主表数据==========================
                        StringBuilder strSql = new StringBuilder();
                        strSql.Append("update ch_hangqing set ");
                        strSql.Append("title = @title,");
                        strSql.Append("update_time = @update_time,");
                        strSql.Append("status = @status,");
                        strSql.Append("hits = @hits,");
                        strSql.Append("sort_id = @sort_id,");
                        strSql.Append("province_id = @province_id,");
                        strSql.Append("city_id = @city_id,");
                        strSql.Append("district_id = @district_id,");
                        strSql.Append("img_url = @img_url,");
                        strSql.Append("caizhi_id = @caizhi_id");
                        strSql.Append(" where id=@id");
                        SqlParameter[] parameters = {
			                    new SqlParameter("@id", SqlDbType.Int,4),
                                new SqlParameter("@title", SqlDbType.NVarChar,100),
                                new SqlParameter("@update_time", SqlDbType.DateTime),
                                new SqlParameter("@status", SqlDbType.TinyInt,1),
                                new SqlParameter("@hits", SqlDbType.Int,4),
                                new SqlParameter("@sort_id", SqlDbType.Int,4),
                                new SqlParameter("@province_id", SqlDbType.Int,4),
                                new SqlParameter("@city_id", SqlDbType.Int,4),
                                new SqlParameter("@district_id", SqlDbType.Int,4),
                                new SqlParameter("@img_url", SqlDbType.NVarChar,255),
                                new SqlParameter("@caizhi_id", SqlDbType.Int,4)};
                        parameters[0].Value = model.id;
                        parameters[1].Value = model.title;
                        parameters[2].Value = model.update_time;
                        parameters[3].Value = model.status;
                        parameters[4].Value = model.hits;
                        parameters[5].Value = model.sort_id;
                        parameters[6].Value = model.province_id;
                        parameters[7].Value = model.city_id;
                        parameters[8].Value = model.district_id;
                        parameters[9].Value = model.img_url;
                        parameters[10].Value = model.caizhi_id;

                        DbHelperSQL.ExecuteSql(conn, trans, strSql.ToString(), parameters);
                        #endregion

                        #region 修改商品规格==========================
                        //先删除旧的规格
                        DbHelperSQL.ExecuteSql(conn, trans, "delete from ch_hangqing_price where article_id=" + model.id);
                        if (model.price != null)
                        {
                            StringBuilder strSql6;
                            foreach (Model.hangqing_price modelt in model.price)
                            {
                                strSql6 = new StringBuilder();
                                strSql6.Append("insert into ch_hangqing_price(");
                                strSql6.Append("article_id,title,price,is_up,sort_id)");
                                strSql6.Append(" values (");
                                strSql6.Append("@article_id,@title,@price,@is_up,@sort_id)");
                                SqlParameter[] parameters6 = {
					                    new SqlParameter("@article_id", SqlDbType.Int,4),
                                        new SqlParameter("@title", SqlDbType.NVarChar,100),
                                        new SqlParameter("@price", SqlDbType.NVarChar,50),
					                    new SqlParameter("@is_up", SqlDbType.Int,4),
					                    new SqlParameter("@sort_id", SqlDbType.Int,4)};

                                parameters6[0].Value = model.id;
                                parameters6[1].Value = modelt.title;
                                parameters6[2].Value = modelt.price;
                                parameters6[3].Value = modelt.is_up;
                                parameters6[4].Value = modelt.sort_id;
                                DbHelperSQL.GetSingle(conn, trans, strSql6.ToString(), parameters6); //带事务
                            }
                        }
                        #endregion

                        trans.Commit();
                    }
                    catch
                    {
                        trans.Rollback();
                        return false;
                    }
                }
            }
            return true;
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int id)
        {
            //删除商品规格
            StringBuilder strSql6 = new StringBuilder();
            strSql6.Append("delete from ch_hangqing_price");
            strSql6.Append(" where article_id=@article_id");
            SqlParameter[] parameters6 = {
					new SqlParameter("@article_id", SqlDbType.Int,4)};
            parameters6[0].Value = id;
            List<CommandInfo> sqllist = new List<CommandInfo>();
            CommandInfo cmd = new CommandInfo(strSql6.ToString(), parameters6);
            sqllist.Add(cmd);

            //删除主表
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from ch_hangqing ");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)};
            parameters[0].Value = id;
            cmd = new CommandInfo(strSql.ToString(), parameters);
            sqllist.Add(cmd);

            int rowsAffected = DbHelperSQL.ExecuteSqlTran(sqllist);
            if (rowsAffected > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.hangqing GetModel(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select top 1 id,title,update_time,status,hits,sort_id,province_id,city_id,district_id,img_url,caizhi_id");
            strSql.Append(" from ch_hangqing");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)};
            parameters[0].Value = id;

            Model.hangqing model = new Model.hangqing();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" id,title,update_time,status,hits,sort_id,province_id,city_id,district_id,img_url,caizhi_id");
            strSql.Append(" FROM ch_hangqing ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得查询分页数据
        /// </summary>
        public DataSet GetList(int pageSize, int pageIndex, string strWhere, string filedOrder, out int recordCount)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * FROM ch_hangqing");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            recordCount = Convert.ToInt32(DbHelperSQL.GetSingle(PagingHelper.CreateCountingSql(strSql.ToString())));
            return DbHelperSQL.Query(PagingHelper.CreatePagingSql(recordCount, pageSize, pageIndex, strSql.ToString(), filedOrder));
        }
        #endregion

        #region 扩展方法============================
        /// <summary>
        /// 返回标题名称
        /// </summary>
        public string GetTitle(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select top 1 title from ch_hangqing");
            strSql.Append(" where id=" + id);
            string title = Convert.ToString(DbHelperSQL.GetSingle(strSql.ToString()));
            if (string.IsNullOrEmpty(title))
            {
                return "-";
            }
            return title;
        }

        /// <summary>
        /// 修改一列数据
        /// </summary>
        public void UpdateField(int id, string strValue)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update ch_hangqing set " + strValue);
            strSql.Append(" where id=" + id);
            DbHelperSQL.ExecuteSql(strSql.ToString());
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.hangqing DataRowToModel(DataRow row)
        {
            Model.hangqing model = new Model.hangqing();
            if (row != null)
            {
                if (row["id"] != null && row["id"].ToString() != "")
                {
                    model.id = int.Parse(row["id"].ToString());
                }
                if (row["title"] != null)
                {
                    model.title = row["title"].ToString();
                }
                if (row["update_time"] != null && row["update_time"].ToString() != "")
                {
                    model.update_time = DateTime.Parse(row["update_time"].ToString());
                }
                if (row["status"] != null && row["status"].ToString() != "")
                {
                    model.status = int.Parse(row["status"].ToString());
                }
                if (row["hits"] != null && row["hits"].ToString() != "")
                {
                    model.hits = int.Parse(row["hits"].ToString());
                }
                if (row["sort_id"] != null && row["sort_id"].ToString() != "")
                {
                    model.sort_id = int.Parse(row["sort_id"].ToString());
                }
                if (row["province_id"] != null && row["province_id"].ToString() != "")
                {
                    model.province_id = int.Parse(row["province_id"].ToString());
                }
                if (row["city_id"] != null && row["city_id"].ToString() != "")
                {
                    model.city_id = int.Parse(row["city_id"].ToString());
                }
                if (row["district_id"] != null && row["district_id"].ToString() != "")
                {
                    model.district_id = int.Parse(row["district_id"].ToString());
                }
                if (row["img_url"] != null)
                {
                    model.img_url = row["img_url"].ToString();
                }
                if (row["caizhi_id"] != null && row["caizhi_id"].ToString() != "")
                {
                    model.caizhi_id = int.Parse(row["caizhi_id"].ToString());
                }
                //附件信息
                model.price = new hangqing_price().GetList(model.id);
            }
            return model;
        }
        #endregion
    }
}
